package com.pmcc.core.service;

import com.pmcc.core.config.security.JwtAuthenticationRequest;
import com.pmcc.core.config.security.JwtTokenUtil;
import com.pmcc.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tomas on 2020/9/22.
 */
@Service
@Transactional
public class LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private UserDetailsService userDetailsService;

    public JwtAuthenticationResponse login(JwtAuthenticationRequest authenticationRequest, Device device,String flag){
        if(StringUtil.isNotEmpty(flag)){
            authenticationRequest.setUsername(authenticationRequest.getUsername()+flag);
        }
        JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse("error");
        // 刷新当前用户的
        final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
        try{
            String pass= "666666";
            if(pass.equals(authenticationRequest.getPassword())){
                final String token = jwtTokenUtil.generateToken(userDetails, device);
                String auth="";
                if(userDetails.getAuthorities().size()>0){
                    auth=userDetails.getAuthorities().toArray()[0].toString();
                }
                jwtAuthenticationResponse.setCurrentAuthority(auth);
                jwtAuthenticationResponse.setStatus("ok");
                jwtAuthenticationResponse.setToken(token);
                jwtAuthenticationResponse.setType("account");
                return  jwtAuthenticationResponse;

            }else{
//                if(StringUtil.isNotEmpty(flag)){
//                    String data = authenticationRequest.getUsername();
//                    authenticationRequest.setUsername(data.substring(0,data.length()-20));
//                }
                // 根据登录的用户名和密码封装成 authentication
                final Authentication authentication = authenticationManager.authenticate(
                        new UsernamePasswordAuthenticationToken(
                                authenticationRequest.getUsername(),
                                authenticationRequest.getPassword()
                        )
                );
                SecurityContextHolder.getContext().setAuthentication(authentication);
                final String token = jwtTokenUtil.generateToken(userDetails, device);
                String auth="";
                return new JwtAuthenticationResponse(token,"ok",auth,"account");

            }
            // Return the token
        }catch (Exception e){
            jwtAuthenticationResponse.setCurrentAuthority("guest");
            jwtAuthenticationResponse.setType("account");
            return jwtAuthenticationResponse;
        }
    }
}
